package day13.Ti.synchronizedDome;

import java.util.concurrent.locks.ReentrantLock;

public class Ti03 {
    public static void main(String[] args) {
        MyRunnable3 mr = new MyRunnable3();
        Thread t1 = new Thread(mr, "窗口一");
        Thread t2 = new Thread(mr, "窗口二");
        Thread t3 = new Thread(mr, "窗口三");
        t1.start();
        t2.start();
        t3.start();
    }
}

class MyRunnable3 implements Runnable {
    private int ticket = 100;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (ticket <= 0) {
                break;
            }else {
                try {
                    Thread.sleep(100);
                    ticket--;
                    System.out.println(Thread.currentThread().getName()+"在卖票还剩"+ticket);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    }
}
